package com.jie.deletefile.FileComent;

import android.util.Log;

import java.io.File;
import java.util.ArrayList;

/**
 * Created by 13952137957 on 2016/8/23.
 */
public class FileManager {

    static FileManager fileManager = new FileManager();
    private String TAG = "TAG";

    public static FileManager getFileManager() {
        return fileManager;
    }

    private FileManager() {
    }

    private ArrayList<FileInfo> anyFileList = new ArrayList<>();
    private long anyFileSize;

    /**
     * 视频文件集合
     */
    private ArrayList<FileInfo> videoFileList = new ArrayList<>();
    private long videoFileSize;
    /**
     * 音频文件集合
     */
    private ArrayList<FileInfo> audioFileList = new ArrayList<>();
    private long audioFileSize;
    /**
     * 图片文件集合
     */
    private ArrayList<FileInfo> imageFileList = new ArrayList<>();
    private long imageFileSize;
    /**
     * 压缩包文件集合
     */
    private ArrayList<FileInfo> zipFileList = new ArrayList<>();
    private long zipFileSize;
    /**
     * apk文件集合
     */
    private ArrayList<FileInfo> apkFileList = new ArrayList<>();
    private long apkFileSize;
    /**
     * 文档文件集合
     */
    private ArrayList<FileInfo> txtFileList = new ArrayList<>();
    private long txtFileSize;


    private static File inSdcardDir;
    private static File outSdcardDir;

    static {
        if (MemoryManager.getPhoneInSDCardPath() != null) {
            inSdcardDir = null;
            inSdcardDir = new File(MemoryManager.getPhoneInSDCardPath());
        }
        if (MemoryManager.getPhoneOutSDCardPath() != null) {
            outSdcardDir = null;
            outSdcardDir = new File(MemoryManager.getPhoneOutSDCardPath());
        }
    }

    private boolean isStopSearch;

    private void initData() {
        isStopSearch = false;
        anyFileSize = 0;
        txtFileSize = 0;
        videoFileSize = 0;
        audioFileSize = 0;
        imageFileSize = 0;
        zipFileSize = 0;
        apkFileSize = 0;
        anyFileList.clear();
        txtFileList.clear();
        videoFileList.clear();
        audioFileList.clear();
        imageFileList.clear();
        zipFileList.clear();
        apkFileList.clear();
    }

    private long getFileSize(File file ) {
        long size = 0;
        if (!file.isDirectory()) {
            size += file.length();
        }
        File files[] = file.listFiles();
        for (int i = 0; i < files.length; i++) {
            size += getFileSize(files[i]);
        }
        return size;

    }

    //递归
    private void searchFile(File file, boolean isFirstRunFlag) {

        if (isStopSearch) {

            if (isFirstRunFlag) {
                callbackSearchFileListenerEnd(true);
            }
            return;
        }

        if (file == null || !file.canRead() || !file.exists()) {
            if (isFirstRunFlag) {
                callbackSearchFileListenerEnd(true);
                return;
            }

        }
        if (!file.isDirectory()) {
            if (file.length() <= 0) {
                return;
            }
            if (file.getName().lastIndexOf(".") == -1) {
                return;

            }

            String[] iconAndTypeName = FileTypeUtil.getFileIconAndTypeName(file);
            String iconName = iconAndTypeName[0];
            String typeName = iconAndTypeName[1];
            FileInfo fileInfo = new FileInfo(file, iconName, typeName);
            anyFileList.add(fileInfo);
            anyFileSize += file.length();
            if (typeName.equals(FileTypeUtil.TYPE_ANY)) {
                anyFileSize += file.length();
                anyFileList.add(fileInfo);

            }else if (typeName.equals(FileTypeUtil.TYPE_APK)) {
                apkFileSize += file.length();
                apkFileList.add(fileInfo);

            }else if (typeName.equals(FileTypeUtil.TYPE_AUDIO)) {
                audioFileList.add(fileInfo);
                audioFileSize += file.length();
            }else if (typeName.equals(FileTypeUtil.TYPE_IMAGE)) {
                imageFileList.add(fileInfo);
                imageFileSize += file.length();
            }else if (typeName.equals(FileTypeUtil.TYPE_TXT)) {
                txtFileList.add(fileInfo);
                txtFileSize += file.length();
            }else if (typeName.equals(FileTypeUtil.TYPE_VIDEO)) {
                videoFileList.add(fileInfo);
                videoFileSize += file.length();
            }else if (typeName.equals(FileTypeUtil.TYPE_ZIP)) {
                zipFileList.add(fileInfo);
                zipFileSize += file.length();
            }
            callbackSearchFileListenerSearching(typeName);
            return;
        }
        File[] files = file.listFiles();
        if (files == null || files.length <= 0) {
            return;
        }
        for (int i = 0; i < files.length; i++) {
            File telFile = files[i];
            searchFile(telFile, false);
        }
        Log.e(TAG, "searchFile: 1111111");
        if (isFirstRunFlag) {
            Log.e(TAG, "searchFile: 搜索完毕");
            callbackSearchFileListenerEnd(false);
        }

    }

    public SearchFileListener listener;

    public void searchSDCradFile() {
        if (anyFileList == null || anyFileList.size() <= 0) {
            initData();
            if (outSdcardDir!=null)
                searchFile(outSdcardDir, false);

            searchFile(inSdcardDir, true);

        } else {
            callbackSearchFileListenerEnd(false);
        }
    }

    public interface SearchFileListener {
        void searching(String typeName);
        void end(boolean isExceptionEnd);
    }

    public void setSearchFileListener(SearchFileListener listener) {
        this.listener = listener;
    }
    public void  callbackSearchFileListenerSearching(String typeName) {
        if (listener != null) {
            listener.searching(typeName);

        }
    }

    public void callbackSearchFileListenerEnd(boolean isExceptionEnd) {
        if (listener != null) {
            listener.end(isExceptionEnd);
        }
    }

    public ArrayList<FileInfo> getAnyFileList() {
        return anyFileList;
    }

    public void setAnyFileList(ArrayList<FileInfo> anyFileList) {
        this.anyFileList = anyFileList;
    }

    public long getAnyFileSize() {
        return anyFileSize;
    }

    public void setAnyFileSize(long anyFileSize) {
        this.anyFileSize = anyFileSize;
    }

    public ArrayList<FileInfo> getVideoFileList() {
        return videoFileList;
    }

    public void setVideoFileList(ArrayList<FileInfo> videoFileList) {
        this.videoFileList = videoFileList;
    }

    public long getVideoFileSize() {
        return videoFileSize;
    }

    public void setVideoFileSize(long videoFileSize) {
        this.videoFileSize = videoFileSize;
    }

    public ArrayList<FileInfo> getAudioFileList() {
        return audioFileList;
    }

    public void setAudioFileList(ArrayList<FileInfo> audioFileList) {
        this.audioFileList = audioFileList;
    }

    public long getAudioFileSize() {
        return audioFileSize;
    }

    public void setAudioFileSize(long audioFileSize) {
        this.audioFileSize = audioFileSize;
    }

    public ArrayList<FileInfo> getImageFileList() {
        return imageFileList;
    }

    public void setImageFileList(ArrayList<FileInfo> imageFileList) {
        this.imageFileList = imageFileList;
    }

    public long getImageFileSize() {
        return imageFileSize;
    }

    public void setImageFileSize(long imageFileSize) {
        this.imageFileSize = imageFileSize;
    }

    public ArrayList<FileInfo> getZipFileList() {
        return zipFileList;
    }

    public void setZipFileList(ArrayList<FileInfo> zipFileList) {
        this.zipFileList = zipFileList;
    }

    public long getZipFileSize() {
        return zipFileSize;
    }

    public void setZipFileSize(long zipFileSize) {
        this.zipFileSize = zipFileSize;
    }

    public ArrayList<FileInfo> getApkFileList() {
        return apkFileList;
    }

    public void setApkFileList(ArrayList<FileInfo> apkFileList) {
        this.apkFileList = apkFileList;
    }

    public long getApkFileSize() {
        return apkFileSize;
    }

    public void setApkFileSize(long apkFileSize) {
        this.apkFileSize = apkFileSize;
    }

    public ArrayList<FileInfo> getTxtFileList() {
        return txtFileList;
    }

    public void setTxtFileList(ArrayList<FileInfo> txtFileList) {
        this.txtFileList = txtFileList;
    }

    public long getTxtFileSize() {
        return txtFileSize;
    }

    public void setTxtFileSize(long txtFileSize) {
        this.txtFileSize = txtFileSize;
    }

    public static File getInSdcardDir() {
        return inSdcardDir;
    }

    public static void setInSdcardDir(File inSdcardDir) {
        FileManager.inSdcardDir = inSdcardDir;
    }

    public static File getOutSdcardDir() {
        return outSdcardDir;
    }

    public static void setOutSdcardDir(File outSdcardDir) {
        FileManager.outSdcardDir = outSdcardDir;
    }

    public boolean isStopSearch() {
        return isStopSearch;
    }

    public void setIsStopSearch(boolean isStopSearch) {
        this.isStopSearch = isStopSearch;
    }
}
